package com.lintf.framework;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @ system:Mac
 * @ author:lin tingfeng
 * @ data:2023/12/8 09:08
 * @ package_name:com.lintf.framework
 * @ project_name:MobileProject
 * @ desc: Druid数据库密码加密
 */
public class DruidEncrypt {

    public static void main(String[] args) throws Exception {
        /*ConfigTools.main(new String[]{"Aliyunmysql123!"});
        String password = ConfigTools.decrypt(
                "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALizFQBZnHsPpj31Z8yOrrRL4R1jtrOnuEdW1Vt2vSKR/qRMqXjVeirWf8PT7srD33T8VuXzdwZpyhWVACDL1oUCAwEAAQ==",
                "aye2YQjlbm/M6p0iwQkGz9/cTLPuhvh6QegsssUJpjHfHU79mmRaDCY15ngDrsKSI8VFTaZMQPbhC7s2lKy8eA==");
        System.out.println("解密密码：" + password);*/
        test();
    }

    public static void test() {
        String password = "Aliyunmysql123!";
        try {
            // 利用阿里的ConfigTools工具类来生成一对公私钥，私钥用来加密，公钥用来解密
            String[] keyParis = ConfigTools.genKeyPair(512);
            String privateKey = keyParis[0];
            String publicKey = keyParis[1];
            System.out.println("privateKey=" + privateKey);
            System.out.println("publicKey=" + publicKey);
            String encryptPassword = ConfigTools.encrypt(privateKey, password);
            System.out.println("encryptPassword=" + encryptPassword);
            String decryptPassword = ConfigTools.decrypt(publicKey, encryptPassword);
            System.out.println("decryptPassword=" + decryptPassword);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
